<?php

namespace App\Http\Controllers\Strator;

use App\Http\Controllers\Controller;
use App\Models\Strator\Strator;
use App\Models\Strator\StratorHasPermission;
use App\Models\Strator\StratorHasPosition;
use App\Models\Strator\StratorPositionHasPermission;
use App\Services\Strator\StratorPermissionService;
use Illuminate\Http\Request;

class StratorAuthController extends Controller
{


    //
    public function authPermissions(Request $request){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator=Strator::findOrFail($request->strator_id);
        if($strator->is_super==1){
            return $this->fail('超级管理员无需授权');
        }
        $strator_id = $request->strator_id;
        $permission_ids = $request->permission_ids;
        if(empty($permission_ids)){
            StratorHasPermission::where('strator_id',$strator_id)->delete();
            return $this->successMsg();
        }
        if(!is_array($permission_ids)){
            return $this->fail('权限id数组格式不正确');
        }
        StratorHasPermission::where('strator_id',$strator_id)
            ->whereNotIn('permission_id',$permission_ids)
            ->delete();
        foreach ($permission_ids as $permission_id){
            StratorHasPermission::updateOrCreate([
                'strator_id'=>$strator_id,
                'permission_id'=>$permission_id
            ]);
        }
        return $this->successMsg();
    }
    //
    public function directPermissionIds(Request $request, StratorPermissionService $permissionService){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator=Strator::findOrFail($request->strator_id);
        $permission_ids=$permissionService->getDirectPermissionIds($strator,1);
        return $this->success($permission_ids);
    }
    //
    public function directPermissions(Request $request, StratorPermissionService $permissionService){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator=Strator::findOrFail($request->strator_id);
        $permissions=$permissionService->getDirectPermissions($strator,1);
        return $this->success($permissions);
    }

    //
    public function authPositions(Request $request){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator=Strator::findOrFail($request->strator_id);
        if($strator->is_super==1){
            return $this->fail('超级管理员无需授权');
        }
        $strator_id = $request->strator_id;
        $position_ids = $request->position_ids;
        if(empty($position_ids)){
            StratorHasPosition::where('strator_id',$strator_id)->delete();
            return $this->successMsg();
        }
        if(!is_array($position_ids)){
            return $this->fail('职务id数组格式不正确');
        }
        StratorHasPosition::where('strator_id',$strator_id)
            ->whereNotIn('position_id',$position_ids)
            ->delete();
        foreach ($position_ids as $position_id){
            StratorHasPosition::updateOrCreate([
                'strator_id'=>$strator_id,
                'position_id'=>$position_id
            ]);
        }
        return $this->successMsg();
    }
    //
    public function hasPositionIds(Request $request){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator_id = $request->strator_id;
        $position_ids = StratorHasPosition::where('strator_id',$strator_id)->pluck('position_id');
        return $this->success($position_ids);
    }
    //
    public function positionPermissionIds(Request $request, StratorPermissionService $permissionService){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator=Strator::findOrFail($request->strator_id);
        $permission_ids=$permissionService->getPositionPermissionIds($strator,1);
        return $this->success($permission_ids);
    }
    //
    public function positionPermissions(Request $request, StratorPermissionService $permissionService){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator=Strator::findOrFail($request->strator_id);
        $permissions=$permissionService->getPositionPermissions($strator,1);
        return $this->success($permissions);
    }

    //
    public function allPermissionIds(Request $request, StratorPermissionService $permissionService){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator=Strator::findOrFail($request->strator_id);
        $permission_ids=$permissionService->getAllPermissionIds($strator,1);
        return $this->success($permission_ids);
    }
    //
    public function allPermissions(Request $request, StratorPermissionService $permissionService){
        if(!$request->filled('strator_id')){
            return $this->fail('管理员id不能为空');
        }
        $strator=Strator::findOrFail($request->strator_id);
        $missionTree=$permissionService->getAllPermissions($strator,1);
        return $this->success($missionTree);
    }



}
